package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 新增菜品和口味操作
     * 涉及多张表操作需要加上事务注解
     * @param dishDTO
     */
    @Transactional
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null && flavors.size()>0){
            //遍历flavors
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            dishFlavorsMapper.insertBatch(flavors);
        }


    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
         Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        long total = page.getTotal();
        return new PageResult(total,page.getResult());
    }

    /**
     * 多张表的操作
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        /**
         * 1可批量删除
         * 2起售菜品无法删除
         * 3被套餐关联的菜品不能删除
         * 4删除菜品后,关联的口味也得删除
         */
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            Integer status = dish.getStatus();
            if(status.equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        List<Long> setMealIdsByDishIds = setMealDishMapper.getSetMealIdsByDishIds(ids);
        if(setMealIdsByDishIds!=null && setMealIdsByDishIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        dishMapper.deleteByIds(ids);
        dishFlavorsMapper.deleteBatchByDishId(ids);
    }

    @Override

    public void startOrStop(Integer status, Long id) {
        if(status==0){
            //更改包含dishid的套餐状态改为停售
             setmealMapper.setSetmealListByDishId(id);
        }
        Dish build = Dish.builder().id(id).status(status).build();
        dishMapper.update(build);
    }

    @Override
    public DishDTO getDishAndFlavorById(Long id) {
        Dish dish = dishMapper.getById(id);
        List<DishFlavor> flavors = dishFlavorsMapper.getFlavorsByDishId(id);
        DishDTO dishDTO = new DishDTO();
        BeanUtils.copyProperties(dish,dishDTO);
        dishDTO.setFlavors(flavors);
        return dishDTO;
    }

    @Transactional
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        dishFlavorsMapper.deleteByDishId(dishDTO.getId());
        //将dishDTO.getFlavors()放入到List<DishFlavor>中.并在每个DishFlavor中添加id:dishDTO.getId()
        dishDTO.getFlavors().forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));

        dishFlavorsMapper.insertBatch(dishDTO.getFlavors());

    }

    @Override
    public List<Dish> getDishByCategoryList(Long categoryId) {
        List<Dish> dish = dishMapper.getDishByCategoryList(categoryId);
        return dish;
    }

    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> listWithFlavor = dishMapper.getListWithFlavor(dish);
        return listWithFlavor;
    }

}
